package team.nine.kuaichezuche.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import team.nine.kuaichezuche.service.VehicleClassificationService;

import java.util.HashMap;
import java.util.Map;

/**
 * 车辆信息分类控制器，处理前端提交的混合车辆信息请求
 */
@RestController
@RequestMapping("/api/vehicle")
public class VehicleClassificationController {

    private final VehicleClassificationService vehicleClassificationService;

    public VehicleClassificationController(VehicleClassificationService vehicleClassificationService) {
        this.vehicleClassificationService = vehicleClassificationService;
    }

    /**
     * 处理前端提交的混合车辆信息
     * @param mixedVehicleData 包含混合车辆信息的Map对象
     * @return 处理结果，包含成功或失败信息，以及相关的ID
     */
    @PostMapping("/submit-mixed-data")
    public ResponseEntity<Map<String, Object>> submitMixedVehicleData(@RequestBody Map<String, Object> mixedVehicleData) {
        Map<String, Object> response = new HashMap<>();

        try {
            // 调用服务层处理混合车辆信息
            Map<String, Object> processResult = vehicleClassificationService.processMixedVehicleData(mixedVehicleData);

            if ((boolean) processResult.getOrDefault("success", false)) {
                // 处理成功
                response.put("status", "success");
                response.put("message", processResult.get("message"));
                response.put("vehicleInfoId", processResult.get("vehicleInfoId"));
                response.put("vehicleInstanceId", processResult.get("vehicleInstanceId"));
                return ResponseEntity.ok(response);
            } else {
                // 处理失败
                response.put("status", "error");
                response.put("message", processResult.getOrDefault("message", "处理失败"));
                return ResponseEntity.badRequest().body(response);
            }

        } catch (Exception e) {
            // 捕获并处理异常
            response.put("status", "error");
            response.put("message", "服务器错误: " + e.getMessage());
            return ResponseEntity.status(500).body(response);
        }
    }
}